﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Solitaire {
    class Slot {
        public Vector2 Position {
            get { return position; }
            set { position = value;
                  rect.X = (int)value.X;
                  rect.Y = (int)value.Y;
            }
        }
        Vector2 position;

        public Rectangle Rect {
            get { return rect; }
            set { rect = value; }
        }
        protected Rectangle rect;

        protected List<Card> slot;
        private Texture2D emptySlot;

        public List<Card> toList() {
            return slot;
        }

        public Slot(Vector2 pos) {
            slot = new List<Card>();
            this.position = pos;
            rect = new Rectangle((int)pos.X, (int)pos.Y, Card.width, Card.height);
        }

        public Slot(): this(new Vector2(0,0)) { }

        public void LoadContent(ContentManager content) {
            emptySlot = content.Load<Texture2D>("empty");
            foreach (Card card in slot) {
                card.LoadContent(content);
            }
        }

        public int Count() {
            return slot.Count;
        }

        public void addCard(Card c) {
            slot.Add(c);
        }

        public Card remCard() {
            Card tmpCard = slot.ElementAt(slot.Count - 1);
            slot.RemoveAt(slot.Count-1);
            return tmpCard;
        }

        public List<Card> remCards(Card c) {
            if (c == null)
                return null;

            List<Card> toRem = new List<Card>();

            slot.FindIndex(0, 1, c.matches());


            return toRem;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch) {
            Card tmpCard;

            if (slot.Count == 0) {
                if(emptySlot != null)
                    spriteBatch.Draw(emptySlot, new Rectangle((int)position.X, (int)position.Y, Card.width, Card.height), Color.White);
            } else {
                for (int i = 0; i < slot.Count; i++) {
                    tmpCard = slot.ElementAt<Card>(i);
                    tmpCard.setPosition(position);
                    tmpCard.Draw(gameTime, spriteBatch);
                }
            }
        }
    }
}
